package com.example.service.impl;

import com.example.pojo.queryResult;
import com.example.pojo.tMesProQualityIndexPojo;
import com.example.pojo.tMesProQualityPojo;
import com.example.mapper.engineDataQueryMapp;
import com.example.mapper.tMesProQualityIndexMapp;
import com.example.service.engineDataQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class engineDataQueryImpl implements engineDataQueryService {
    @Autowired
    private engineDataQueryMapp engineDataQueryMapp;
    @Autowired
    private tMesProQualityIndexMapp tMesProQualityIndexMapp;

    //查询发动机力矩信息--多条件查询
    public queryResult queryMultipleConditions(Map<String, Object> map) {
        String engineNumber = (String) map.get("engineNumber");
        String stationId = (String) map.get("stationId");
        String timeStart = (String) map.get("timeStart");
        String timeEnd = (String) map.get("timeEnd");
        Integer page = (Integer) map.get("page");
        Integer sizePage = (Integer) map.get("sizePage");
        Integer startPage = (page - 1) * sizePage;

        //多条件查询主表数据，是否存在，将结果返回在list中
        List<tMesProQualityPojo> partList = engineDataQueryMapp.queryMultipleConditions(engineNumber, stationId, timeStart, timeEnd);

        //调出分表索引记录中的分表的表名，返回在list中，用于分表查询
        List<tMesProQualityIndexPojo> nameList = tMesProQualityIndexMapp.queryTableName();

        //循环查询分表中的内容-根据查到的索引表中的名称
        for (int i = 0; i < nameList.size(); i++) {
            //每个表一次开始查询
            List<tMesProQualityPojo> pageList = engineDataQueryMapp.queryMultipleConditionsPage(engineNumber, stationId, timeStart, timeEnd, nameList.get(i).getTable_name());
            //每次查询到数据分表中查询结果添加到主数据中
            partList.addAll(pageList);
        }

        //实现数据分页功能，得到起始和结束的数据行，防止索引超越限制
        int endIndex = Math.min(startPage + sizePage, partList.size());

        // 获取分页后的子列表
        List<tMesProQualityPojo> paginatedList = partList.subList(startPage, endIndex);

        //组织数据到设定的格式内容中，将结果统一返回
        queryResult queryResult = new queryResult();
        queryResult.setData(paginatedList);
        queryResult.setTotal(partList.size());

        return queryResult;
    }

}
